package main

import (
	_ "github.com/Go-SQL-Driver/MySQL"
	"database/sql"
	"log"
)

//事物回滚

func main() {

	db, err := sql.Open("mysql", "root:root@(localhost:3306)/godb?charset=utf8")
	if err != nil {
		log.Fatalln("db connect wrong")
	}

	defer db.Close()

	tx, _ := db.Begin()

	stmt, err := tx.Prepare("INSERT INTO userinfo values (null, ?, ?, ?)")
	if err != nil {
		log.Fatalln("db Prepare insert wrong")
	}
	result, err := stmt.Exec("astaxie", "RD_DEPT", "2013-12-09")
	if err != nil {
		log.Fatalln("stmt Exec wrong")
	}
	id, err := result.LastInsertId()
	log.Println(id)

	if id >= 1 {
		tx.Commit()
	} else {
		panic("insert failed")
	}


	//tx.Rollback()
	//tx.Commit()


}

/*

CREATE TABLE `userinfo` (
  `uid` int(10) NOT NULL AUTO_INCREMENT,
  `username` varchar(64) DEFAULT NULL,
  `departname` varchar(64) DEFAULT NULL,
  `created` date DEFAULT NULL,
  PRIMARY KEY (`uid`)
) ENGINE=InnoDB AUTO_INCREMENT=8 DEFAULT CHARSET=utf8;

*/